//
//  ViewController.m
//  Alltelevisionnews
//
//  Created by dllo on 15/11/26.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UIScrollViewDelegate, NewsTableViewDelegate>

@property(nonatomic, strong) UIScrollView *pageScrollView;
@property(nonatomic, strong) UIScrollView *titleScrollView;
@property(nonatomic, strong) NSArray *titleArray;
@property(nonatomic, assign) NSInteger buttonTag;
@property(nonatomic, strong) UITableView *tableView;
@property(nonatomic, strong) NSString *strURL;
@property(nonatomic, assign) NSInteger page;
@property(nonatomic, strong) NSMutableArray *array;
@property(nonatomic, strong) NSArray *urlArr;
@property(nonatomic, assign) NSInteger count;
@property(nonatomic, strong) NSString *str;
@property(nonatomic, strong) MBProgressHUD *hud;
@property(nonatomic, strong) NSMutableArray *photoArray;
@property(nonatomic, strong) NSMutableArray *namearray;
@property(nonatomic, strong) SLBannerView *banner;
@property(strong, nonatomic) NSArray *bannerImageURLArray;
@property(strong, nonatomic) NSArray *bannerTitleStrArray;
@property(nonatomic, strong) NSArray *ARR;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    // Do any additional setup after loading the view, typically from a nib.
    //创建左侧抽屉的点击滑出button
    UIButton *menuBtu = [UIButton buttonWithType:UIButtonTypeCustom];
    menuBtu.frame = CGRectMake(0, 0, 20, 18);
    [menuBtu setBackgroundImage:[UIImage imageNamed:@"menu"] forState:UIControlStateNormal];
    [menuBtu addTarget:self action:@selector(openOrCloseLeftList) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:menuBtu];
    
    //判断模式
    NSString *sandBox = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *docPath = [sandBox stringByAppendingPathComponent:@"button.plist"];
    
    NSString *Str = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
    
    if ([Str isEqualToString:@"确认"]) {
        
        [ZDQSingleton shareSingleton].isLongin = YES;
        
    } else {
        
        [ZDQSingleton shareSingleton].isLongin = NO;
        [self.tableView reloadData];
        
    }
        
    self.titleArray = @[@"头条", @"娱乐", @"财经", @"军事", @"体育", @"游戏", @"数码", @"科技", @"汽车"];
    
    self.titleScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(80 * KWIDTH / 375, 0, 280 * KWIDTH / 375, 40 * KHEIGHT / 667)];
    self.navigationItem.titleView = self.titleScrollView;
    self.titleScrollView.contentSize = CGSizeMake(280 * KWIDTH / 375 * 2.3, 0);
    
    for (NSInteger i = 0; i < self.titleArray.count; i++) {
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.titleScrollView addSubview:button];
        button.frame = CGRectMake((60 * KWIDTH / 375 * i) + (10 * KWIDTH / 375 * i), 0, 60, 30 * KHEIGHT / 667);
        [button setTitle:self.titleArray[i] forState:UIControlStateNormal];
        button.tag = 1000 + i;
        button.titleLabel.textColor = [UIColor whiteColor];
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        
        UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake((60 * KWIDTH / 375 * i) + (10 * KWIDTH / 375 * i), 33, 60, 2 * KHEIGHT / 667)];
        [self.titleScrollView addSubview:titleView];
        titleView.backgroundColor = [UIColor clearColor];
        titleView.tag = 2000 + i;
        
    }
    
    UIButton *button = (UIButton *)[self.titleScrollView viewWithTag:1000];
    [button setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    UIView *titleView = (UIView *)[self.titleScrollView viewWithTag:2000];
    titleView.backgroundColor = [UIColor orangeColor];
    
#pragma mark 创建pageScrollView;
    self.pageScrollView = [[UIScrollView alloc] init];
    [self.view addSubview:self.pageScrollView];
    self.pageScrollView.contentSize = CGSizeMake(self.titleArray.count * KWIDTH, 0);
    self.pageScrollView.pagingEnabled = YES;
    [self.pageScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.and.left.and.bottom.and.right.equalTo(self.view).insets(UIEdgeInsetsMake(0, 0, 0, 0));
        
    }];
    
    for (NSInteger i = 0; i < self.titleArray.count; i++) {
        
        NewsTableView *news = [[NewsTableView alloc] initWithFrame:CGRectMake(KWIDTH * i, 0, KWIDTH, KHEIGHT - 60 * KHEIGHT / 667) style:UITableViewStylePlain];
        news.separatorStyle = NO;
        [self.pageScrollView addSubview:news];
        news.tag = 3000 + i;
        
        UIView *hearderView = [[UIView alloc] initWithFrame:CGRectMake(KWIDTH * i, 0, KWIDTH, 200 * KHEIGHT / 667)];
        news.tableHeaderView = hearderView;
        //        hearderView.backgroundColor = [UIColor redColor];
        hearderView.tag = 4000 + i;
        
        if ([ZDQSingleton shareSingleton].isLongin == YES) {
            news.backgroundColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.3 alpha:1];
            hearderView.backgroundColor = [UIColor blackColor];
        } else {
            news.backgroundColor = [UIColor whiteColor];
            hearderView.backgroundColor = [UIColor whiteColor];
            
        }

        
    }
    self.pageScrollView.delegate = self;
    
    //数组初始化
    self.array = [NSMutableArray array];
    
    self.urlArr = @[@"id=SYLB10,SYDT10,SYRECOMMEND", @"id=YL53,FOCUSYL53", @"id=CJ33,FOCUSCJ33", @"id=JS83,FOCUSJS83", @"id=TY43,FOCUSTY43,TYLIVE", @"id=YX11,FOCUSYX11", @"id=SM66,FOCUSSM66", @"id=KJ123,FOCUSKJ123", @"id=QC45,FOCUSQC45", @"id=SS78,FOCUSSS78"];
    
     UIView *hearderView = (UIView *)[self.pageScrollView viewWithTag:4000 + self.pageScrollView.contentOffset.x / KWIDTH];
    self.banner = [SLBannerView bannerWithFrame:hearderView.frame actionBlock:^(NSInteger index) {
        
        ZDQNewsWebTableViewController *newsWebVC = [[ZDQNewsWebTableViewController alloc] init];
        [self.navigationController pushViewController:newsWebVC animated:YES];
        NewsModel *model = self.ARR[index];
        newsWebVC.StrURL = model.commentsUrl;
        
    }];
    [hearderView addSubview:self.banner];
    
    self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.hud.labelText = @"正在加载...";
    
    [self addhearder];
    [self addfooter];
    
}


#pragma mark 上拉加载下拉刷新的方法

- (void)addhearder {
    
    NSInteger i = self.pageScrollView.contentOffset.x / KWIDTH;
    
    NewsTableView *news = (NewsTableView *)[self.pageScrollView viewWithTag:3000 + self.pageScrollView.contentOffset.x / KWIDTH];
    news.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page = 1;
        self.strURL = [NSString stringWithFormat:@"http://api.iclient.ifeng.com/ClientNews?%@&page=%ld&gv=4.4.9", self.urlArr[i], self.page];
//        NSLog(@"%@", self.strURL);
        [self create];
        [news.mj_header endRefreshing];
        
    }];
    [news.mj_header beginRefreshing];
}

- (void)addfooter {
    
    NSInteger i = self.pageScrollView.contentOffset.x / KWIDTH;
    NewsTableView *news = (NewsTableView *)[self.pageScrollView viewWithTag:3000 + self.pageScrollView.contentOffset.x / KWIDTH];
    news.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.page = self.page + 1;
        self.strURL = [NSString stringWithFormat:@"http://api.iclient.ifeng.com/ClientNews?%@&page=%ld&gv=4.4.9", self.urlArr[i], (long)self.page];
        
        [self footerCreate];
        [news.mj_footer endRefreshing];
        
    }];
}

#pragma mark下拉刷新的数据解析方法
- (void)create {
    
    [SLAFNetWorkTool cacheGetWithURL:self.strURL cookie:nil success:^(id responseObject) {
        
        NSArray *Array = responseObject;
        if (self.array.count != 0) {
            [self.array removeAllObjects];
        }
        NSArray *arr = [NewsModel baseModelByArr:Array[0][@"item"]];
        [self.array addObjectsFromArray: arr];
        NewsTableView *news = (NewsTableView *)[self.pageScrollView viewWithTag:3000 + self.pageScrollView.contentOffset.x / KWIDTH];
        news.Array = self.array;
        news.dataSource = news;
        news.iddelegate = self;
        news.delegate = news;

        
        if ([Array count] == 0) {
            [self addhearder];
            [self addfooter];
        } else {
            
        self.ARR = [NewsModel baseModelByArr:Array[1][@"item"]];
            
        }
        self.photoArray = [NSMutableArray array];
        self.namearray = [NSMutableArray array];
        
        for (NewsModel *model in self.ARR) {
            
            [self.photoArray addObject:model.thumbnail];
            [self.namearray addObject:model.title];
            
        }
        
        self.bannerImageURLArray = [self.photoArray copy];
        self.bannerTitleStrArray = [self.namearray copy];
        
        self.banner.imageURLStrArray = self.bannerImageURLArray;
        self.banner.titleStrArray = self.bannerTitleStrArray;
        
        [self.hud removeFromSuperview];
        [news reloadData];
        
    } failure:^(NSError *error) {
    }];
}



#pragma mark 上拉加载的数据解析方法
- (void)footerCreate {
    
    [SLAFNetWorkTool cacheGetWithURL:self.strURL cookie:nil success:^(id responseObject) {
        
        NSArray *Array = responseObject;
        if (Array.count == 0) {
        } else {
        NSArray *arr = [NewsModel baseModelByArr:Array[0][@"item"]];
        [self.array addObjectsFromArray: arr];
        }
        NewsTableView *news = (NewsTableView *)[self.pageScrollView viewWithTag:3000 + self.pageScrollView.contentOffset.x / KWIDTH];
        news.Array = self.array;
        news.dataSource = news;
        news.delegate = news;
        news.iddelegate = self;
        
        [news reloadData];
        
    } failure:^(NSError *error) {
    }];
}



#pragma mark tableView的点击方法
- (void)didSelectRowAtIndexPath:(NSIndexPath *)indexPath tableView:(UITableView *)tableView {
    
    NewsTableView *news = (NewsTableView *)[self.pageScrollView viewWithTag:3000 + self.pageScrollView.contentOffset.x / KWIDTH];
    NewsModel *model = self.array[indexPath.row];
    if (tableView == news) {
        
        if ([model.type isEqualToString:@"topic2"]) {
            ZDQTopicViewController *topicVC = [[ZDQTopicViewController alloc] init];
            [self.navigationController pushViewController:topicVC animated:YES];
            topicVC.strURL = model.detallsID;
            
        } else {
            
            ZDQNewsWebTableViewController *newsWeb = [[ZDQNewsWebTableViewController alloc] init];
            [self.navigationController pushViewController:newsWeb animated:YES];
            newsWeb.StrURL = model.commentsUrl;
            
        }
     }
    
}



#pragma mark 标题button的点击方法
- (void)buttonAction:(UIButton *)button
{
    
    UIButton *titlebutton = (UIButton *)[self.titleScrollView viewWithTag:1000];
    [titlebutton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    UIView *View = (UIView *)[self.titleScrollView viewWithTag:2000];
    View.backgroundColor = [UIColor clearColor];
    //判断前一个button和View的颜色
    if (self.buttonTag != 0 && self.buttonTag != button.tag) {
        UIButton *clickButton = (UIButton *)[self.titleScrollView viewWithTag:self.buttonTag];
        [clickButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        UIView *titleView = (UIView *)[self.titleScrollView viewWithTag:2000 + (self.buttonTag - 1000)];
        titleView.backgroundColor = [UIColor clearColor];
        
    }
    
    self.buttonTag = button.tag;
    
    UIButton *changebutton = (UIButton *)[self.titleScrollView viewWithTag:button.tag];
    [changebutton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    UIView *titleView = (UIView *)[self.titleScrollView viewWithTag:2000 + (button.tag - 1000)];
    titleView.backgroundColor = [UIColor orangeColor];
    //设置页面的偏移量
    self.pageScrollView.contentOffset = CGPointMake(KWIDTH * (button.tag - 1000), 0);
    if (self.buttonTag > 1001) {
        self.titleScrollView.contentOffset = CGPointMake((self.buttonTag - 1002) * 70, 0);
    }
    
    UIView *hearderView = (UIView *)[self.pageScrollView viewWithTag:4000 + self.pageScrollView.contentOffset.x / KWIDTH];
    self.banner = [SLBannerView bannerWithFrame:hearderView.frame actionBlock:^(NSInteger index) {
        
        ZDQNewsWebTableViewController *newsWebVC = [[ZDQNewsWebTableViewController alloc] init];
        [self.navigationController pushViewController:newsWebVC animated:YES];
        NewsModel *model = self.ARR[index];
        newsWebVC.StrURL = model.commentsUrl;
        
    }];
    [hearderView addSubview:self.banner];
    
    [self addhearder];
    [self addfooter];
}




#pragma mark - scrollView的协议方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    [self addhearder];
    [self addfooter];
    
    UIButton *titlebutton = (UIButton *)[self.titleScrollView viewWithTag:1000];
    [titlebutton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    UIView *View = (UIView *)[self.titleScrollView viewWithTag:2000];
    View.backgroundColor = [UIColor clearColor];
    if (self.buttonTag != 0) {
        
        UIButton *clickButton = (UIButton *)[self.titleScrollView viewWithTag:self.buttonTag];
        [clickButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        UIView *titleView = (UIView *)[self.titleScrollView viewWithTag:2000 + (self.buttonTag - 1000)];
        titleView.backgroundColor = [UIColor clearColor];
        
    }
    
    self.buttonTag = 1000 + self.pageScrollView.contentOffset.x / KWIDTH;
    
    self.titleScrollView.contentOffset = CGPointMake(self.pageScrollView.contentOffset.x / KWIDTH, 0);
    UIButton *button= (UIButton *)[self.titleScrollView viewWithTag:1000 + self.pageScrollView.contentOffset.x / KWIDTH];
    [button setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    UIView *view = (UIView *)[self.titleScrollView viewWithTag:2000 + self.pageScrollView.contentOffset.x / KWIDTH];
    view.backgroundColor = [UIColor orangeColor];
    
    if (self.buttonTag > 1001) {
        self.titleScrollView.contentOffset = CGPointMake((self.buttonTag - 1002) * 70, 0);
    }
    
    
    UIView *hearderView = (UIView *)[self.pageScrollView viewWithTag:4000 + self.pageScrollView.contentOffset.x / KWIDTH];
    self.banner = [SLBannerView bannerWithFrame:hearderView.frame actionBlock:^(NSInteger index) {
        
        ZDQNewsWebTableViewController *newsWebVC = [[ZDQNewsWebTableViewController alloc] init];
        [self.navigationController pushViewController:newsWebVC animated:YES];
        NewsModel *model = self.ARR[index];
        newsWebVC.StrURL = model.commentsUrl;
        
    }];
    [hearderView addSubview:self.banner];
    
}






#pragma mark button的点击方法
- (void)openOrCloseLeftList {
    
    AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    if (tempAppDelegate.leftslideVC.closed) {
        
        [tempAppDelegate.leftslideVC openLeftView];
        
    } else {
        
        [tempAppDelegate.leftslideVC closeLeftView];
        
    }
    
}




- (void)viewWillDisappear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [tempAppDelegate.leftslideVC setPanEnabled:NO];
    
    
}



- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    AppDelegate *temAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [temAppDelegate.leftslideVC setPanEnabled:YES];
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
